//
//  CompanyDetailViewController.m
//  kr_36
//
//  Created by lmt on 16/1/28.
//  Copyright © 2016年 www.lanou3g.com 蓝鸥科技. All rights reserved.
//

#import "CompanyDetailViewController.h"
#import "LMTAFNetwork.h"
#import <UIImageView+WebCache.h>
#import "CompanyBaseModel.h"

@interface CompanyDetailViewController ()

@property (nonatomic, retain) UIImageView *beijing;/**< 背景图片 */

@property (nonatomic, retain) UIImageView *logo;/**< 公司Logo */
@property (nonatomic, retain) UILabel *name;/**< 公司名称 */
@property (nonatomic, retain) UILabel *brief;/**< 公司介绍 */
@property (nonatomic, retain) UILabel *address;/**< 公司地址 */
@property (nonatomic, retain) UILabel *biaoqian;/**< 公司标签 */
@property (nonatomic, retain) UIImageView *biao;/**< 标签图片 */

@property (nonatomic, retain) NSMutableDictionary *baseDic;/**< 基本信息数据 */

@end

@implementation CompanyDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationController.navigationBarHidden = YES;
    
    
    // 背景
    self.beijing = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 350)];
    _beijing.image = [UIImage imageNamed:@"momohu"];
    [self.view addSubview:_beijing];
    
    //  创建需要的毛玻璃特效类型
    UIBlurEffect *blurEffect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleExtraLight];
    //  毛玻璃view 视图
    UIVisualEffectView *effectView = [[UIVisualEffectView alloc] initWithEffect:blurEffect];
    //添加到要有毛玻璃特效的控件中
    effectView.frame = _beijing.bounds;
    [_beijing addSubview:effectView];
    //设置模糊透明度
    effectView.alpha = 0.6;
    
    _beijing.userInteractionEnabled = YES;
    
    // 公司数据处理
    [self dataCompany];
    
    // 返回
    UIButton *back = [UIButton buttonWithType:(UIButtonTypeCustom)];
    back.frame = CGRectMake(5, 20, 30, 30);
    [back setImage:[UIImage imageNamed:@"back@2x"] forState:(UIControlStateNormal)];
    [_beijing addSubview:back];
    [back addTarget:self action:@selector(back:) forControlEvents:(UIControlEventTouchUpInside)];
    
    // 分享
    UIButton *share = [UIButton buttonWithType:(UIButtonTypeCustom)];
    share.frame = CGRectMake(self.view.frame.size.width - 40, 20, 30, 30);
    [share setImage:[UIImage imageNamed:@"share@2x"] forState:(UIControlStateNormal)];
    [_beijing addSubview:share];
    [share addTarget:self action:@selector(share:) forControlEvents:(UIControlEventTouchUpInside)];
    
}

- (void)dataCompany
{
    NSString *str = [NSString stringWithFormat:@"https://rong.36kr.com/api/mobi/company/%@/info", self.cid];
    NSLog(@"%@", str);
    
    [LMTAFNetwork getUrl:str body:nil response:LMTJSON requestHeadFile:nil success:^(NSURLSessionDataTask *task, id resposeObject) {
        
        // 保存基本信息数据
        self.baseDic = resposeObject[@"data"][@"base"];
        NSLog(@"%@", _baseDic);
        // 通过基本信息数据创建视图
        
        // logo 创建
        self.logo = [[UIImageView alloc] initWithFrame:CGRectMake(0, 40, 70, 70)];
        _logo.center = CGPointMake(self.view.center.x, _logo.center.y);
        [_logo sd_setImageWithURL:[NSURL URLWithString:_baseDic[@"logo"]]];
        _logo.layer.cornerRadius = 35;
        _logo.clipsToBounds = YES;
        [_beijing addSubview:_logo];
        
        // name 创建
        // 计算name 宽度
        NSDictionary *attributes = @{NSFontAttributeName:[UIFont systemFontOfSize:17],};
        CGSize text = [_baseDic[@"name"] boundingRectWithSize:CGSizeMake(100, 100) options:(NSStringDrawingTruncatesLastVisibleLine) attributes:attributes context:nil].size;
        self.name = [[UILabel alloc] initWithFrame:CGRectMake(0, 115, text.width, 30)];
        _name.center = CGPointMake(self.view.center.x, _name.center.y);
        _name.text = _baseDic[@"name"];
        _name.textColor = [UIColor whiteColor];
        [_beijing addSubview:_name];
        
        // brief创建
        self.brief = [[UILabel alloc] initWithFrame:CGRectMake(0, 150, 300, 40)];
        _brief.numberOfLines = 0;
        _brief.center = CGPointMake(self.view.center.x, _brief.center.y);
        _brief.textAlignment = NSTextAlignmentCenter;
        _brief.font = [UIFont systemFontOfSize:15];
        _brief.textColor = [UIColor whiteColor];
        _brief.text = _baseDic[@"brief"];
        [_beijing addSubview:_brief];
        
        // address 创建
        // 计算address宽度
        text = [_baseDic[@"location"] boundingRectWithSize:CGSizeMake(400, 400) options:(NSStringDrawingTruncatesLastVisibleLine) attributes:attributes context:nil].size;
        self.address = [[UILabel alloc] initWithFrame:CGRectMake(0, 195, text.width, 30)];
        _address.center = CGPointMake(self.view.center.x, _address.center.y);
        _address.text = _baseDic[@"location"];
        _address.font = [UIFont systemFontOfSize:13];
        _address.textAlignment = NSTextAlignmentCenter;
        _address.textColor = [UIColor grayColor];
        [_beijing addSubview:_address];
        
        // 标签创建
        NSArray *arr = _baseDic[@"tags"];
        NSString *biaoStr = [arr componentsJoinedByString:@" "];
        NSLog(@"%@", biaoStr);
        self.biaoqian = [[UILabel alloc] initWithFrame:CGRectMake(0, 230, self.view.frame.size.width, 30)];
        _biaoqian.textColor = [UIColor grayColor];
        _biaoqian.font = [UIFont systemFontOfSize:13];
        _biaoqian.textAlignment = NSTextAlignmentCenter;
        [_beijing addSubview:_biaoqian];
        _biaoqian.text = biaoStr;
        
        // 关注按钮
        UIButton *buttonGuan = [UIButton buttonWithType:(UIButtonTypeCustom)];
        buttonGuan.frame = CGRectMake(0, 270, 120, 50);
        [buttonGuan setTitle:@"+  关注" forState:(UIControlStateNormal)];
        [_beijing addSubview:buttonGuan];
        buttonGuan.backgroundColor = [UIColor redColor];
        [buttonGuan setTitleColor:[UIColor whiteColor] forState:(UIControlStateNormal)];
        [buttonGuan addTarget:self action:@selector(guanzhu:) forControlEvents:(UIControlEventTouchUpInside)];
        buttonGuan.center = CGPointMake(self.view.center.x, buttonGuan.center.y);
        buttonGuan.layer.cornerRadius = 12;
        buttonGuan.clipsToBounds = YES;
        
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        
    }];
    
}

- (void)share:(UIButton *)btn
{
    
}

- (void)guanzhu:(UIButton *)btn
{
    
}

- (void)back:(UIButton *)btn
{
    self.navigationController.navigationBarHidden = NO;
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
